﻿function initFolderList() {
    cms.DnD.ClearDropZones();
    // Make the domain the first dropzone
    var domain = document.getElementById("domain");
    cms.DnD.AddDropZone(domain);
    iLib.Dom.AddListener(domain, "mouseover", function (e) {
        if (cms.DnD.GetIsDragging())
            return;
        e = e || window.event;
        var target = e.target || e.srcElement;
        while (target.id != "domain")
            target = target.parentNode;
        if (target.className != "selected")
            target.className = "hover";
    });
    iLib.Dom.AddListener(domain, "mouseout", function (e) {
        if (cms.DnD.GetIsDragging())
            return;
        e = e || window.event;
        var target = e.target || e.srcElement;
        while (target.id != "domain")
            target = target.parentNode;
        if (target.className != "selected")
            target.className = "";
    });
    iLib.Dom.AddListener(domain, "click", function (e) {
        e = e || window.event;
        var target = e.target || e.srcElement;
        while (target.id != "domain")
            target = target.parentNode;
        setSelectedFolder(target);
    });
    // Get all folder spans
    var spans = document.getElementById("folderPane").getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].parentNode.tagName == "LI") {
            cms.DnD.AddDropZone(spans[i]);
            iLib.Dom.AddListener(spans[i], "mouseover", function (e) {
                if (cms.DnD.GetIsDragging())
                    return;
                e = e || window.event;
                var target = e.target || e.srcElement;
                while (target.parentNode && target.parentNode.tagName != "LI")
                    target = target.parentNode;
                if (target.className != "selected")
                    target.className = "hover";
            });
            iLib.Dom.AddListener(spans[i], "mouseout", function (e) {
                if (cms.DnD.GetIsDragging())
                    return;
                e = e || window.event;
                var target = e.target || e.srcElement;
                while (target.parentNode && target.parentNode.tagName != "LI")
                    target = target.parentNode;
                if (target.className != "selected")
                    target.className = "";
            });
            iLib.Dom.AddListener(spans[i], "mousedown", function (e) {
                cms.DnD.InitDrag("LI", e);
            });
            iLib.Dom.AddListener(spans[i], "click", function (e) {
                e = e || window.event;
                var target = e.target || e.srcElement;
                setSelectedFolder(target);
            });
        }
    }
    // Get all folder icons
    var imgs = document.getElementById("folderPane").getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].parentNode.tagName == "SPAN" && imgs[i].parentNode.id != "domain") {
            iLib.Dom.AddListener(imgs[i], "click", function (e) {
                e = e || window.event;
                var target = e.target || e.srcElement;
                iLib.Dom.PreventBubble(e);
                prepareFolderPopup(target, target.parentNode.id, target.nextSibling.textContent, e);
            });
        }
    }
    // Set up the EndDrag handler
    cms.DnD.AddEndDragHandler(onEndDrag);
    // Get all toggle links
    var links = document.getElementById("folderPane").getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        if (links[i].className != "empty") {
            iLib.Dom.AddListener(links[i], "click", function (e) {
                cms.Lists.ShowPaneOverlay("folderPaneOverlay");
                e = e || window.event;
                var target = e.target || e.srcElement;
                iLib.Dom.PreventBubble(e);
                document.getElementById("hidToggleFolderId").value = target.id.substr(1);
                document.getElementById("btnToggleFolder").click();
                return false;
            });
        }
    }
}

function setSelectedFolder(target) {
    var spans = document.getElementById("folderPane").getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++)
        spans[i].className = "";
    while (target.parentNode.tagName != "LI" && target.id != "domain")
        target = target.parentNode;
    target.className = "selected";
    var hidSelectedFolderID = document.getElementById("hidSelectedFolderID");
    if (target.id == hidSelectedFolderID.value)
        return;
    hidSelectedFolderID.value = target.id;
    cms.Lists.ShowPaneOverlay("pagePaneOverlay");
    document.getElementById("btnSelectFolder").click();
}

function onEndDrag(draggedElement, dropzoneElement, dropzonePart) {
    document.getElementById("hidDraggedID").value = draggedElement.id;
    document.getElementById("hidDropzoneID").value = dropzoneElement != null ? dropzoneElement.id : "";
    if (dropzoneElement != null && draggedElement.id != dropzoneElement.id) {
        cms.Lists.ShowPaneOverlay("folderPaneOverlay");
        cms.Lists.ShowPaneOverlay("pagePaneOverlay");
        // confirmDrag is generated from the code behind to allow localization
        if (confirmDrag) {
            confirmDrag(draggedElement, dropzoneElement);
        }
    }
}

function initPageList() {
    // Get all page spans
    var spans = document.getElementById("pagePane").getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].parentNode.tagName == "TD" && (spans[i].id.indexOf("P") == 0 || spans[i].id.indexOf("A") == 0)) {
            iLib.Dom.AddListener(spans[i], "mousedown", function (e) { cms.DnD.InitDrag("TD", e); });
        }
        else if (spans[i].parentNode.tagName == "SPAN") {
        iLib.Dom.AddListener(spans[i], "click", function (e) {
            e = e || window.event;
            var target = e.target || e.srcElement;
            if (target.tagName == "STRONG")
                target = target.parentNode;
            iLib.Dom.PreventBubble(e);
            var tdName = target.parentNode.parentNode;
            var tdFile = tdName.nextSibling;
            while (tdFile.tagName != "TD")
                tdFile = tdFile.nextSibling;
            var url = tdFile.textContent ? tdFile.textContent : tdFile.innerText;
            if (document.getElementById("hidSelectedFolderPath").value != "")
                url = document.getElementById("hidSelectedFolderPath").value + "/" + url;
            document.getElementById("hidUrl").value = url;
            FileBrowserDialogue.submit()
        });
        }
    }
    // Get all page icons
    var imgs = document.getElementById("pagePane").getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].parentNode.tagName == "SPAN") {
            iLib.Dom.AddListener(imgs[i], "click", function (e) {
                e = e || window.event;
                var target = e.target || e.srcElement;
                iLib.Dom.PreventBubble(e);
                preparePagePopup(target, target.parentNode.id, target.nextSibling.textContent, e);
            });
        }
    }
}

function prepareFolderPopup(trigger, id, name, e) {
    var popup = document.getElementById("pnlFolderPopup");
    var hlOpenFolder = document.getElementById("hlOpenFolder");
    hlOpenFolder.onclick = function (e) {
        var target = trigger.nextSibling;
        setSelectedFolder(target);
        return false;
    };
    var hlEditFolder = document.getElementById("hlEditFolder");
    hlEditFolder.href = "Admin/TinyMCEIntegration/EditFolderPopup.aspx?id=" + id.substring(1);
    document.getElementById("hidDeleteFolderID").value = id;
    cms.Lists.CurrentName = name;
    cms.Lists.ShowPopup(popup, trigger, "left", 0, 0, true, e);
}

function preparePagePopup(trigger, id, name, e) {
    var popup = document.getElementById("pnlPagePopup");
    var hlSelectPage = document.getElementById("hlSelectPage");
    var hlEditPageBasicProperties = document.getElementById("hlEditPageBasicProperties");
    var hlEditPageLocalizedProperties = document.getElementById("hlEditPageLocalizedProperties");
    var hlEditPageDesign = document.getElementById("hlEditPageDesign");
    var hlEditPageCode = document.getElementById("hlEditPageCode");
    var hlEditPageContent = document.getElementById("hlEditPageContent");
    if (id.substring(0, 1) == "P") {
        hlEditPageBasicProperties.href = "Admin/EditPage.aspx?tab=basicproperties&id=" + id.substring(1);
        hlEditPageLocalizedProperties.href = "Admin/EditPage.aspx?tab=localizedproperties&id=" + id.substring(1);
        hlEditPageContent.href = "Admin/EditPage.aspx?tab=content&id=" + id.substring(1);
        hlEditPageDesign.style.display = "none";
        hlEditPageCode.style.display = "none";
    }
    else {
        hlEditPageBasicProperties.href = "Admin/EditApplicationPage.aspx?tab=basicproperties&id=" + id.substring(1);
        hlEditPageLocalizedProperties.href = "Admin/EditApplicationPage.aspx?tab=localizedproperties&id=" + id.substring(1);
        hlEditPageDesign.href = "Admin/EditApplicationPage.aspx?tab=design&id=" + id.substring(1);
        hlEditPageCode.href = "Admin/EditApplicationPage.aspx?tab=code&id=" + id.substring(1);
        hlEditPageContent.href = "Admin/EditApplicationPage.aspx?tab=content&id=" + id.substring(1);
        hlEditPageDesign.style.display = "";
        hlEditPageCode.style.display = "";
    }
    hlSelectPage.onclick = function (e) {
        var target = trigger.nextSibling;
        var tdName = target.parentNode.parentNode;
        var tdFile = tdName.nextSibling;
        while (tdFile.tagName != "TD")
            tdFile = tdFile.nextSibling;
        var url = tdFile.textContent;
        if (document.getElementById("hidSelectedFolderPath").value != "")
            url = document.getElementById("hidSelectedFolderPath").value + "/" + url;
        document.getElementById("hidUrl").value = url;
        FileBrowserDialogue.submit()
        return false;
    };
    document.getElementById("hidSelectedPageID").value = id;
    var listContainer = document.getElementById("listContainer");
    cms.Lists.CurrentName = name;
    cms.Lists.ShowPopup(popup, trigger, "left", 0, 0, true, e);
}

var FileBrowserDialogue;

function initPopup() {
    FileBrowserDialogue = {
        init: function () {
            // Here goes your code for setting your custom things onLoad.
        },
        submit: function () {
            var url = document.getElementById("hidUrl").value;
            var win = tinyMCEPopup.getWindowArg("window");
            // insert information now
            win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = url;
            // are we an image browser
            if (typeof (win.ImageDialog) != "undefined") {
                // we are, so update image dimensions...
                if (win.ImageDialog.getImageData)
                    win.ImageDialog.getImageData();

                // ... and preview if necessary
                if (win.ImageDialog.showPreviewImage)
                    win.ImageDialog.showPreviewImage(url);
            }
            // close popup window
            tinyMCEPopup.close();
        }
    }
    tinyMCEPopup.onInit.add(FileBrowserDialogue.init, FileBrowserDialogue);
}

function createListPopupMenu() {
    $(".popup").each(function () {
        cms.Lists.CreateListPopupMenu(this);
    });
}

$(document).ready(function () {
    iLib.Dom.AddListener(window, "load", initFolderList);
    iLib.Dom.AddListener(window, "load", initPageList);
    iLib.Dom.AddListener(window, "load", initPopup);
    createListPopupMenu();
    cms.Ajax.Init();
    cms.Ajax.AddEndRequestHandler(cms.Lists.HidePaneOverlays);
    cms.Ajax.AddEndRequestHandler(createListPopupMenu);
});